/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.sciTechnologyDemand.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.activity.domain.dto.ActivityEditUsersDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoDto;
import com.fzhucc.modules.sciTechnologyDemand.domain.SciTechnologyDemand;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.*;
import com.fzhucc.modules.sciTechnologyDemand.service.SciTechnologyDemandService;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-11-28
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "大赛企业技术需求管理")
@RequestMapping("/api/sciTechnologyDemand")
public class SciTechnologyDemandController {

    private final SciTechnologyDemandService sciTechnologyDemandService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sciTechnologyDemand:list')")
    public void exportSciTechnologyDemand(HttpServletResponse response, SciTechnologyDemandQueryCriteria criteria) throws IOException {
        sciTechnologyDemandService.download(sciTechnologyDemandService.queryAll(criteria), response);
    }

    @Log("我的导出数据")
    @ApiOperation("我的导出数据")
    @GetMapping(value = "/my/download")
    @PreAuthorize("@el.check('sciTechnologyDemand:list')")
    public void myexportSciTechnologyDemand(HttpServletResponse response, SciTechnologyDemandQueryCriteria criteria) throws IOException {
        sciTechnologyDemandService.download(sciTechnologyDemandService.queryAllmy(criteria), response);
    }

    @GetMapping
    @Log("查询大赛企业技术需求")
    @ApiOperation("查询大赛企业技术需求")
    @PreAuthorize("@el.check('sciTechnologyDemand:list')")
    public ResponseEntity<Object> querySciTechnologyDemand(SciTechnologyDemandQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(sciTechnologyDemandService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/my")
    @Log("我的大赛企业技术需求")
    @ApiOperation("我的大赛企业技术需求")
    //@PreAuthorize("@el.check('sciTechnologyDemand:list')")
    public ResponseEntity<Object> my(SciTechnologyDemandQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(sciTechnologyDemandService.my(criteria, pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询大赛企业技术需求")
    @ApiOperation("h5、小程序查询大赛企业技术需求")
    public ResponseEntity<Object> list(SciTechnologyDemandQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(sciTechnologyDemandService.queryAllh5(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/getDostatusCounts")
    @Log("需求获取分类数据")
    @ApiOperation("需求获取分类数据")
    @AnonymousAccess
    public ResponseEntity<Object> getDostatusCounts(SciTechnologyDemandQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(sciTechnologyDemandService.queryAllByDoStatusCount(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增大赛企业技术需求")
    @ApiOperation("新增大赛企业技术需求")
    @PreAuthorize("@el.check('sciTechnologyDemand:add')")
    public ResponseEntity<Object> createSciTechnologyDemand(@Validated @RequestBody SciTechnologyDemandAddDto dto) {
        return new ResponseEntity<>(sciTechnologyDemandService.create(dto), HttpStatus.CREATED);
    }

    /*@PostMapping(value = "/addWithSaveSciDraft")
    @Log("新增草稿")
    @ApiOperation("新增草稿")
    @PreAuthorize("@el.check('sciTechnologyDemand:add')")
    public ResponseEntity<Object> addWithSaveSciDraft(@Validated @RequestBody SciTechnologyDemandAddDto dto) {
        return new ResponseEntity<>(sciTechnologyDemandService.addWithSaveSciDraft(dto), HttpStatus.CREATED);
    }*/

    @PutMapping
    @Log("修改大赛企业技术需求")
    @ApiOperation("修改大赛企业技术需求")
    @PreAuthorize("@el.check('sciTechnologyDemand:edit')")
    public ResponseEntity<Object> updateSciTechnologyDemand(@Validated @RequestBody SciTechnologyDemandEditDto dto) {
        sciTechnologyDemandService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/updateDoStatus")
    @Log("修改大赛企业技术需求办理状态")
    @ApiOperation("修改大赛企业技术需求办理状态")
    @PreAuthorize("@el.check('sciTechnologyDemand:edit')")
    public ResponseEntity<Object> updateDoStatus(@Validated @RequestBody SciTechnologyDemandEditDoStatusDto dto) {
        sciTechnologyDemandService.updateDoStatus(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除大赛企业技术需求")
    @ApiOperation("删除大赛企业技术需求")
    @PreAuthorize("@el.check('sciTechnologyDemand:del')")
    public ResponseEntity<Object> deleteSciTechnologyDemand(@RequestBody Integer[] ids) {
        sciTechnologyDemandService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/getZqxCompanyType")
    @Log("获取字典zqx_company_type 助企行企业技术需求征集表企业类型")
    @ApiOperation("获取字典zqx_company_type 助企行企业技术需求征集表企业类型")
    @AnonymousAccess
    public ResponseEntity<Object> getZqxCompanyType() {
        return new ResponseEntity<>(sciTechnologyDemandService.getZqxCompanyType(), HttpStatus.OK);
    }

    @GetMapping(value = "/getZqxCompanyArea")
    @Log("获取字典zqx_company_area 助企行企业技术需求征集表企业所在区域")
    @ApiOperation("获取字典zqx_company_area 助企行企业技术需求征集表企业所在区域")
    @AnonymousAccess
    public ResponseEntity<Object> getzqxCompanyArea() {
        return new ResponseEntity<>(sciTechnologyDemandService.getzqxCompanyArea(), HttpStatus.OK);
    }

    @GetMapping(value = "/getZqxMaType")
    @Log("获取字典zqx_ma_type 助企行企业技术需求征集表劳模工匠人才类型")
    @ApiOperation("获取字典zqx_ma_type 助企行企业技术需求征集表劳模工匠人才类型")
    @AnonymousAccess
    public ResponseEntity<Object> getZqxMaType() {
        return new ResponseEntity<>(sciTechnologyDemandService.getZqxMaType(), HttpStatus.OK);
    }

    @GetMapping(value = "/getZqxTechnology")
    @Log("获取字典zqx_technology 助企行企业技术需求征集表所需技术领域")
    @ApiOperation("获取字典zqx_technology 助企行企业技术需求征集表所需技术领域")
    @AnonymousAccess
    public ResponseEntity<Object> getZqxTechnology() {
        return new ResponseEntity<>(sciTechnologyDemandService.getZqxTechnology(), HttpStatus.OK);
    }

    @GetMapping(value = "/getZqxReType")
    @Log("获取字典zqx_re_type 助企行企业技术需求征集表需求类别")
    @ApiOperation("获取字典zqx_re_type 助企行企业技术需求征集表需求类别")
    @AnonymousAccess
    public ResponseEntity<Object> getZqxReType() {
        return new ResponseEntity<>(sciTechnologyDemandService.getZqxReType(), HttpStatus.OK);
    }

    @PostMapping(value = "/addWithSaveSci")
    @Log("小程序、h5新增大赛企业技术需求 并添加保存至助企行大赛系统")
    @ApiOperation("小程序、h5新增大赛企业技术需求 并添加保存至助企行大赛系统")
    public ResponseEntity<Object> createAll(@Validated @RequestBody SciTechnologyDemandAddDto dto) {
        return new ResponseEntity<>(sciTechnologyDemandService.createWithSaveFileWithSci(dto), HttpStatus.CREATED);
    }

    @PutMapping(value = "/audit")
    @Log("大赛企业技术需求状态审核")
    @ApiOperation("大赛企业技术需求状态审核")
    public ResponseEntity<Object> auditSciTechnologyDemand(@Validated @RequestBody AuditSciTechnologyDemandDto dto) {
        sciTechnologyDemandService.auditSciTechnologyDemand(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/isAllowEdit")
    @Log("是否允许编辑")
    @ApiOperation("是否允许编辑")
    public ResponseEntity<Object> isAllowEdit(@Validated @RequestBody AllowSciTechnologyDemandDto dto) {
        sciTechnologyDemandService.isAllowEdit(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个大赛企业技术需求")
    @ApiOperation("查询单个大赛企业技术需求")
    public SciTechnologyDemandDto queryInfoById(@PathVariable Integer id) {
        return sciTechnologyDemandService.findById(id);
    }

    @PostMapping(value = "/getAbutmentData")
    @Log("获取工信厅技术信息")
    @ApiOperation("获取工信厅技术信息")
    public ResponseEntity<Object> createAll(@Validated @RequestBody List<AbutmentDataDto> dto) {
        return new ResponseEntity<>(sciTechnologyDemandService.getAbutmentData(dto), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/bindZqxQyId")
    @Log("劳模根据企业表，同名绑定其id至需求表")
    @ApiOperation("劳模根据企业表，同名绑定其id至需求表")
    public ResponseEntity<Object> bindZqxQyId() {
        sciTechnologyDemandService.bindZqxQyId();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}